/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package be.hogent.designpatterns.billenkar.commands;

import be.hogent.designpatterns.billenkar.Billenkar;
import be.hogent.designpatterns.billenkar.BillenkarVerzameling;
import be.hogent.designpatterns.billenkar.GroteBillenkar;
import be.hogent.designpatterns.billenkar.KleineBillenkar;
import be.hogent.designpatterns.billenkar.decorators.MetBluts;
import be.hogent.designpatterns.billenkar.decorators.MetKrassen;
import be.hogent.designpatterns.billenkar.decorators.MetPlatteBand;
import be.hogent.designpatterns.billenkar.decorators.MetRoest;
import java.util.Scanner;

/**
 *
 * @author Jeroen
 */
public class KiesMankementenCommand implements Command {
    private Billenkar billenkar;
    private Command[] mankementenCommands;
    private Scanner in = new Scanner(System.in);
    private int positie;
    private BillenkarVerzameling billenkarVerzameling;

    public KiesMankementenCommand(Billenkar billenkar,BillenkarVerzameling billenkarVerzameling, int positie) {
        this(billenkar);
        this.positie = positie;
        this.billenkarVerzameling = billenkarVerzameling;
    }

    public KiesMankementenCommand(Billenkar billenkar) {
        this.billenkar = billenkar;
        mankementenCommands = new Command[4];
        mankementenCommands[0] = new VoegRoestToeCommand();
        mankementenCommands[1] = new VoegPlatteBandToeCommand();
        mankementenCommands[2] = new VoegKrassenToeCommand();
        mankementenCommands[3] = new VoegBlutsToeCommand();
        positie = -1;
        billenkarVerzameling = null;
    }

    public void execute()
    {
        int[] mankementen = doeMankementenMenu();
        while(mankementen == null) {
            System.out.println("ONGELDIGE KEUZE!");
            mankementen = doeMankementenMenu();
        }
        for(int i=0;i<mankementen.length;i++) {
            mankementenCommands[mankementen[i]-1].execute();
        }
        if(positie != -1) {
            billenkarVerzameling.update(positie, billenkar);
        }

    }

    private int[] doeMankementenMenu()
    {
         System.out.println("");
         System.out.println("");
         System.out.println("-----------------KIES MANKEMENTEN------------------");
         System.out.println("1. Roest ");
         System.out.println("2. Platte Band ");
         System.out.println("3. Krassen ");
         System.out.println("4. Bluts ");
         System.out.println("(Verschillende mogelijkheden met komma tussen elke keuze)");
         String inv = in.nextLine();
         String[] keuzes = inv.split(",");
         int[] keuzenrs = new int[keuzes.length];
         try {
             for(int i = 0;i<keuzes.length;i++) {
                 keuzenrs[i] = Integer.parseInt(keuzes[i]);
                 if(keuzenrs[i]<1 || keuzenrs[i]>4)
                     throw new NumberFormatException();
             }
         } catch (NumberFormatException e) {
             keuzenrs = null;
         }
        return keuzenrs;
    }

    /**
     * @return the billenkar
     */
    public Billenkar getBillenkar()
    {
        return billenkar;
    }

    public class VoegKrassenToeCommand implements Command {
        public void execute() {
            billenkar = new MetKrassen(getBillenkar());
        }
    }
    public class VoegBlutsToeCommand implements Command {
        public void execute() {
            billenkar = new MetBluts(getBillenkar());
        }
    }
    public class VoegPlatteBandToeCommand implements Command {
        public void execute() {
            billenkar = new MetPlatteBand(getBillenkar());
        }
    }
    public class VoegRoestToeCommand implements Command {
        public void execute() {
            billenkar = new MetRoest(getBillenkar());
        }
    }
}
